<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="产品图片" prop="productImg">
        <UploadImg v-model="formData.productImg" />
      </el-form-item>
      <el-form-item label="产品名称" prop="productName">
        <el-input v-model="formData.productName" placeholder="请输入产品名称" />
      </el-form-item>
<!--      <el-form-item label="图标" prop="iconStr">-->
<!--        <el-input v-model="formData.iconStr" placeholder="请输入图标" />-->
<!--      </el-form-item>-->
      <el-form-item label="最高贷款额度" prop="maxAmount">
        <el-input v-model="formData.maxAmount" type="number" placeholder="请输入最高贷款额度" />
      </el-form-item>
      <el-form-item label="年利息" prop="annualInterest">
        <el-input v-model="formData.annualInterest" placeholder="请输入年利息" />
      </el-form-item>
      <el-form-item label="贷款期限" prop="loanTime">
        <el-input v-model="formData.loanTime" placeholder="请输入贷款期限" />
      </el-form-item>
<!--      <el-form-item label="贷款期限" prop="loanTime">-->
<!--        <el-date-picker-->
<!--          v-model="formData.loanTime"-->
<!--          type="date"-->
<!--          value-format="x"-->
<!--          placeholder="选择贷款期限"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="产品类型" prop="productType">

        <el-select v-model="formData.productType" placeholder="请选择" @change="changes">
          <el-option
            v-for="item in productTypeList"
            :key="item.id"
            :label="item.typeNames"
            :value="item.id"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="产品标签" prop="productLable">
        <el-input v-model="formData.productLable" placeholder="请输入产品标签多个描述使用英文逗号分开例如：下款快,审批快" />
      </el-form-item>

      <el-form-item label="产品介绍" prop="productIntroduce">
        <el-input v-model="formData.productIntroduce" placeholder="请输入产品介绍" />
      </el-form-item>

      <el-form-item label="* 产品倍数" prop="multiple"
                    v-show="formData.productType=='8' || formData.productType=='9' || formData.productType=='10'">
        <el-input v-model="formData.multiple" oninput ="value=value.replace(/[^0-9.]/g,'')" placeholder="请输入倍数" />
      </el-form-item>

      <el-form-item label="产品倍数(X)" prop="portions" v-show="formData.productType=='2' || formData.productType=='4'
      || formData.productType=='11' || formData.productType=='12' || formData.productType=='13' ">
        <el-input v-model="formData.multiple" oninput ="value=value.replace(/[^0-9.]/g,'')" placeholder="产品倍数(X)" />
      </el-form-item>

      <el-form-item label="产品倍数(Y)" prop="multiple" v-show="formData.productType=='2' || formData.productType=='4'
      || formData.productType=='11' || formData.productType=='12'|| formData.productType=='13'">
        <el-input v-model="formData.multipleTwo" oninput ="value=value.replace(/[^0-9.]/g,'')" placeholder="请输入产品倍数(Y)" />
      </el-form-item>


      <el-form-item label="* 产品成数(X)" prop="portions"
                    v-show="formData.productType=='1' || formData.productType=='5' || formData.productType=='7' || formData.productType=='8' || formData.productType=='9' || formData.productType=='10' || formData.productType=='14' || formData.productType=='15' || formData.productType=='16'">
        <el-input v-model="formData.portions" oninput ="value=value.replace(/[^0-9.]/g,'')" placeholzder="请输入产品成数(X)" />
      </el-form-item>

      <el-form-item label="* 产品成数(Y)" prop="portionsTwo"
                    v-show="formData.productType=='1' || formData.productType=='5' || formData.productType=='7' || formData.productType=='8' || formData.productType=='9' || formData.productType=='10' || formData.productType=='14' || formData.productType=='15' || formData.productType=='16'">
        <el-input v-model="formData.portionsTwo" oninput ="value=value.replace(/[^0-9.]/g,'')" placeholder="请输入产品成数(Y)" />
      </el-form-item>


      <el-form-item label="* 产品成数(X1)" prop="alternativePortionsOne" label-width="110px"
                    v-show="formData.productType=='10' || formData.productType=='14'">
        <el-input v-model="formData.alternativePortionsOne" oninput ="value=value.replace(/[^0-9.]/g,'')" placeholder="请输入产品成数(X1)" />
      </el-form-item>

      <el-form-item label="* 产品成数(Y1)" prop="alternativePortionsTwo" label-width="110px"
                    v-show="formData.productType=='10' || formData.productType=='14'">
        <el-input v-model="formData.alternativePortionsTwo" oninput ="value=value.replace(/[^0-9.]/g,'')" placeholder="请输入产品成数(Y1)" />
      </el-form-item>





<!--      <el-form-item label="产品成数" prop="portions"-->
<!--                    v-show="formData.productType=='8'||formData.productType=='9' || formData.productType=='10'">-->
<!--        <el-input v-model="formData.portions" oninput ="value=value.replace(/[^0-9.]/g,'')" placeholder="请输入成数" />-->
<!--      </el-form-item>-->

<!--      <el-form-item label="产品倍数" prop="multiple"-->
<!--                          v-show="formData.productType=='10'">-->
<!--              <el-input v-model="formData.multiple" oninput ="value=value.replace(/[^0-9.]/g,'')" placeholder="请输入倍数" />-->
<!--      </el-form-item>-->



<!--      <el-form-item label="最低产品倍数" prop="multiple"-->
<!--                    v-show="formData.productType=='11'">-->
<!--        <el-input v-model="formData.multiple" oninput ="value=value.replace(/[^0-9.]/g,'')" placeholder="请输入最低倍数" />-->
<!--      </el-form-item>-->

<!--      <el-form-item label="最高产品倍数" prop="multipleTwo"-->
<!--                    v-show="formData.productType=='11'">-->
<!--        <el-input v-model="formData.multipleTwo" oninput ="value=value.replace(/[^0-9.]/g,'')" placeholder="请输入最高倍数" />-->
<!--      </el-form-item>-->

      <el-form-item label="状态" prop="status">
        <el-select v-model="formData.status" clearable placeholder="请选择状态">
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.COMMON_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
import { CommonStatusEnum } from '@/utils/constants'
import * as ProductApi from '@/api/hutai/product'
import * as PublicityTypeApi from '@/api/hutai/publicityType'

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  productName: undefined,
  iconStr: undefined,
  maxAmount: undefined,
  annualInterest: undefined,
  loanTime: undefined,
  productType: undefined,
  productLable: undefined,
  productIntroduce: undefined,
  productImg: undefined,
  portions: undefined,
  portionsTwo: undefined,
  alternativePortionsOne:undefined,
  alternativePortionsTwo:undefined,
  multiple: undefined,
  multipleTwo: undefined,
  status: undefined
})
const formRules = reactive({
  productName: [{ required: true, message: '产品名称不能为空', trigger: 'blur' }],
  maxAmount: [{ required: true, message: '最高贷款额度不能为空', trigger: 'blur' }],
  annualInterest: [{ required: true, message: '年利率不能为空', trigger: 'blur' }],
  loanTime: [{ required: true, message: '贷款期限不能为空', trigger: 'blur' }],
  productType: [{ required: true, message: '产品类型不能为空', trigger: 'blur' }],
  productLable: [{ required: true, message: '产品标签不能为空', trigger: 'blur' }],
  productIntroduce: [{ required: true, message: '产品介绍不能为空', trigger: 'blur' }],
  status: [{ required: true, message: '帐号状态（0正常 1停用）不能为空', trigger: 'blur' }]
})
const formRef = ref() // 表单 Ref
const productTypeList = ref([]) // 类型列表
/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await ProductApi.getProduct(id)
    } finally {
      formLoading.value = false
    }
  }
  // 类型列表
  productTypeList.value = await PublicityTypeApi.getPublicityTypeList();
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  if (!formRef) return
  const valid = await formRef.value.validate()
  if (!valid) return
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as ProductApi.ProductVO
    if (formType.value === 'create') {
      await ProductApi.createProduct(data)
      message.success(t('common.createSuccess'))
    } else {
      await ProductApi.updateProduct(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

const changes = () => {
  console.log(formData.value.productType)
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    productName: undefined,
    iconStr: undefined,
    maxAmount: undefined,
    annualInterest: undefined,
    loanTime: undefined,
    productType: undefined,
    productLable: undefined,
    productIntroduce: undefined,
    productImg: undefined,
    status: undefined
  }
  formRef.value?.resetFields()
}
</script>
